using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Objects : MonoBehaviour
{
	public enum BehaviorType
	{	
		cuberoid,
		attackcube
	}
	
	public class ObjectBase
	{
		public BehaviorType ObjectBehavior;	
		public GameObject Self; 
		public bool exists = true;
		
		public bool behindCamera()
		{
			if (Self.transform.position.z < -5)
				return true;
			else
				return false;
		}
	}
	
	public static int ObjectsCount;
	public static List<ObjectBase> EntList = new List<ObjectBase>();
	public static void insertToList(ObjectBase new_ent) {EntList.Add(new_ent);}
	
	public class Cuberoid : ObjectBase
	{
		public Cuberoid(BehaviorType new_behavior)
		{
			// CONSTRUCTOR
			Self = GameObject.CreatePrimitive(PrimitiveType.Cube);
			Self.renderer.material.color = new Color(Random.value, Random.value, Random.value, 1);
			
			ObjectBehavior = new_behavior;
			ObjectsCount++;
			
			insertToList(this);
			
			Self.transform.localScale = new Vector3(Random.Range(3, 8), Random.Range (3, 8), Random.Range(3, 8));
		}
		
		// DESTRUCTOR
		~Cuberoid() {Objects.ObjectsCount--;}
	}
	
	public static ObjectBase[] QueueDelete = new ObjectBase[5];
	public int QDC;
	
	void Update()
	{
		if (EntList.Count > 0)
		{
			QDC = 0;
			foreach (ObjectBase CurObj in EntList)
			{
				
				switch (CurObj.ObjectBehavior)
				{
					//CUBEROID BEHAVIOUR
				case BehaviorType.cuberoid:
					CurObj.Self.transform.position += new Vector3(0, 0, -0.05f);
					if (CurObj.behindCamera())
					{
						Destroy(CurObj.Self);
						QueueDelete[QDC] = CurObj; QDC++;
					}
					
					break;
					
					//DEFAULT / ERROR
				default:
					QueueDelete[QDC] = CurObj; QDC++;
					Destroy(CurObj.Self);
					break;
				}
			}
			
			if (QDC >= 0)
			{
				for (int i = 0; i < QDC; i++)
				{
					ObjectBase ObjectThatsGoingToBeDeleted = QueueDelete[i];
					if (ObjectThatsGoingToBeDeleted != null)
					{
						EntList.Remove(ObjectThatsGoingToBeDeleted);
					}
				}
			}
		}
	}
}
